<template>
  <div>
    <!-- 货运单查询——交货单页面-->
    <TopFilter ref="topfrom" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>
    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="deliverOrderListIndex"
        :isSelectColumn="true"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isForbidden">生成货运单</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(1)">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>
    <Drawer :visible.sync="addVisible" :title="'生成货运单'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <addModel :fromData="formData" @close="addcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getDeliverOrderList, exportList } from '@/api/omSale/OrderManagement/ShipmentOrderQuery';
import addModel from './addModel';
import { exportExcelFile } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'deliverOrderListIndex',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      fetchApi: getDeliverOrderList,
      formData: {},
      exportLoading: false,
      addVisible: false,
      selectList: [],
      isForbidden: true
    };
  },
  components: {
    addModel
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    async addHandle(type) {
      if (type == 1) {

        // let json = Object.assign({}, { CODE: 'CODE_0000038' }, this.fetchParams);
        // let res = await exporturl(json);
        // if (res.resultCode == 200) {
        //   this.$router.push('/admin/auth/oem/downloadCenter');
        // }

       this.$refs.topfrom.SUBMIT_FORM();

        this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
          confirmButtonText: '下载',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let json = this.fetchParams;
            this.exportLoading = true;
            exportList(json)
              .then(response => {
                exportExcelFile(response.data, '货运单查询');
                this.exportLoading = false;
              })
              .catch(err => {
                console.log(err);
              });
          })
          .catch(() => {
            console.log('456');
            this.$message({
              type: 'info',
              message: '已取消'
            });
          });
      } else if (type === 2) {
        if (this.selectList.length > 0) {
          let checkFailedVins = this.checkCertificateMessage();
          if (checkFailedVins.length > 0) {
            await this.$confirm('车辆{'+checkFailedVins+'}缺少合格证信息是否继续生成货运单?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(async () => {
                //数据校验
                this.checkSelectData();
                if (this.isForbidden == false) {
                    this.formData = this.buildCreateShipmentOrderData();
                    this.addVisible = true;
                }
              })
              .catch(() => {});
          } else {
            //数据校验
            this.checkSelectData();
            if (this.isForbidden == false) {
                this.formData = this.buildCreateShipmentOrderData();
                this.addVisible = true;
            }
          }
        }
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    selectChangeHandle(row) {
      this.selectList = row;
      if (this.selectList.length > 0) {
        this.isForbidden = false;
      } else {
        this.isForbidden = true;
      }
      console.log('选中数据',this.selectList);
    },
    //生成货运单校验
    checkSelectData() {
      let checkMassage;
      for (let SelectData of this.selectList) {
        //是否为同一经销商
        if(SelectData.dealerCode != this.selectList[0].dealerCode) {
          this.isForbidden = true;
          checkMassage = '存在非同一经销商的交货单'
        }
        //是否为统一收车地址
        if(SelectData.addressId != this.selectList[0].addressId) {
          this.isForbidden = true;
          checkMassage = '存在非同一收车地址的交货单'
        }
        //发运状态是否为待配板
        if(SelectData.transStatus != 24211001) {
          this.isForbidden = true;
          checkMassage = '存在货运状态非待配板的交货单'
        }
      }
      if (checkMassage) {
        this.$notify({
          title: '警告',
          message: checkMassage,
          type: 'warning',
        });
      } else {
          this.isForbidden = false;
      } 
    },
    //合格证校验
    checkCertificateMessage() {
      let checkFailedVins = [];
      this.selectList.map(x=>{
        if(x.certificateNo == null || x.certificateNo == '') {
          checkFailedVins.push(x.vin);
        }
      });
      console.log('合格证信息缺失',checkFailedVins);
      return checkFailedVins;
    },
    //货运单页面所需数据构造
    buildCreateShipmentOrderData() {
      let data = {};
      data.dealerCode = this.selectList[0].dealerCode;
      data.dealerName = this.selectList[0].dealerName;
      data.addressId = this.selectList[0].addressId;
      data.address = this.selectList[0].address;
      data.deliveryCityName = this.selectList[0].deliveryCityName;
      data.linkMan = this.selectList[0].linkMan;
      data.linPhone = this.selectList[0].linPhone;
      data.orderList  = this.selectList;
      return data;
    },
    addcloseHandler(val) {
      this.addVisible = false;
      if (val) {
        console.log('返回',val);
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    resetChange(val) {
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2,
        },
        {
          type: 'INPUT',
          label: '货运单号',
          fieldName: 'shipmentOrderNo',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: 'SAP销售单号',
          fieldName: 'sapNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '货运状态',
          fieldName: 'transStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2421')
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '货运状态',
          dataIndex: 'transStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('2421'),
          render: (props) => {
            return <span>{this.createDictText(props.row.transStatus, '2421')}</span>;
          },
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '货运单号',
          dataIndex: 'shipmentOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '交货单号',
          dataIndex: 'sapDeliveryOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'SAP销售单号',
          dataIndex: 'sapNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'DMS批售订单号 ',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单管理号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          },
        },
        {
          title: '发运须知',
          dataIndex: 'deliveryRamark',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '发车基地',
          dataIndex: 'carrierBase',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车地址id',
          dataIndex: 'addressId',
          sorter: true,
          filter: true,
          hidden:true,
          filterType: 'input',
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '送达城市',
          dataIndex: 'deliveryCityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物流公司',
          dataIndex: 'carrierCompany',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '车牌号',
          dataIndex: 'trailerBrand',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '板次',
          dataIndex: 'frequency',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员姓名',
          dataIndex: 'carrierDriver',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员电话',
          dataIndex: 'carrierDriverPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员身份证号',
          dataIndex: 'carrierDriverIdCard',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '发车时间',
          dataIndex: 'carrierDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车人',
          dataIndex: 'receiver',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车时间',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '是否三方车辆',
          dataIndex: 'isTripartite',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span>{this.createDictText(p.row.isTripartite, '1004')}</span>;
          },
        },
        {
          title: '合格证编号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '合格证打印时间',
          dataIndex: 'certificatePrintDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '配置',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ];
    },
  },
};
</script>